using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;

namespace WindowsApplication4
{
    public static class App
    {
        public enum TimeFormat
        {
            Local =0,
            Server=1
        }

       public static string version = "1.0";

        public static TimeFormat TimeFormatToUse
        {
            get
            {
                return (TimeFormat)WindowsApplication4.Properties.Settings.Default.TimeFormat;
            }
            set
            {
                WindowsApplication4.Properties.Settings.Default.TimeFormat =(int) value;
            }
        }

        public class CONSTS
        {
            public class MapHighlights
            {
                public class Columns
                {
                    public static int TribeIDIndex = 0;
                    public static int PlayerNameIndex = 1;
                    public static int ColorIndex = 2;

                }
            }
            //public static string File = @".\data\w3.xml";

        }
        private static Point mapCenter = new Point(Convert.ToInt32(WindowsApplication4.Properties.Settings.Default.centerX)
            , Convert.ToInt32(WindowsApplication4.Properties.Settings.Default.centerY));
        public static int mapZoom = Convert.ToInt32(WindowsApplication4.Properties.Settings.Default.zoomski);
        public static bool drawAbondonedVillage = true;
        public static bool drawNonSelected = true;
        public static Color nonSelectedColor = WindowsApplication4.Properties.Settings.Default.abondonedColor;

        public static DataTable dtMapHighlights;

        public static Compaigns compaigns;

        //public static DataSet dsCompaign;
        //public static DataTable dtCompaigns;
        //public static DataTable dtTargets;
        //public static DataTable dtAttacks;


        static App()
        {
            DataColumn col;

            dtMapHighlights = new DataTable("MapHighlights");
            col = dtMapHighlights.Columns.Add("TribeID", System.Type.GetType("System.Int32"));
            col.AllowDBNull = true;
            dtMapHighlights.Columns.Add("PlayerName", System.Type.GetType("System.String"));
            dtMapHighlights.Columns.Add("Color", System.Type.GetType("System.Int32"));

            compaigns = new Compaigns();

        }


        public static event AppUpdatedDelegate AppUpdatedEvent;

        public static Point MapCenter
        {
            get
            {
                return mapCenter;
            }
            set
            {
                mapCenter = value;
                AppUpdatedEvent();
            }
        }

        public static int TimeAdjustment {
            get
            {
                return Convert.ToInt32(WindowsApplication4.Properties.Settings.Default.TimeAdjustment);
            }
            set 
            {
                WindowsApplication4.Properties.Settings.Default.TimeAdjustment = Convert.ToDecimal( value );
            }
        }


        public static string FormatTWEventTime(DateTime time)
        {
            //if (time.Millisecond >= 500)
            //{
            //    time = time.Add(new TimeSpan(0, 0, 0, 1));
            //}

            if (TimeFormatToUse == TimeFormat.Server)
            {
                time = time.Add(new TimeSpan(TimeAdjustment, 0, 0));
            }


            string s;
           // TimeSpan diff = time.Subtract(DateTime.Now);
            
            //if (diff.TotalDays >= 0 && diff.TotalDays <= 1)
            
            if (time.Date  == DateTime.Today)
            {
                s = "today at " + time.ToString("HH:mm:ss");
            }
            //else if (diff.TotalDays >= 0 && diff.TotalDays <= 2)
            //{
            //    s = "tomorrow at " + time.ToString("HH:mm:ss");
            //}
            else
            {
                s = time.ToString("MMM dd HH:mm:ss");
            }
            return s;
        }

        public static string FormatTWDuration(TimeSpan time)
        {
            //if (time.Milliseconds >= 500)
            //{
            //    time = time.Add(new TimeSpan(0,0,0,1));
            //}

            string s = "";
            if (time.TotalHours > 0)
            {
                s += Math.Floor(time.TotalHours) + ":";
            }
            s += (time.Minutes < 10 ? "0" + time.Minutes.ToString() : time.Minutes.ToString())
                + ":" + (time.Seconds < 10 ? "0" + time.Seconds.ToString() : time.Seconds.ToString());

            return s;
        }

        public static string FormatVillageForDisplay(Village vill)
        {
            string s = "";
            if (vill != null)
            {
                Player player = vill.VillageOwner;
                s = vill.name + " - (" + vill.X.ToString() + "|" + vill.y.ToString() + ")";
                s += "\nPoints: " + vill.Points.ToString();
                if (player != null)
                {
                    s += "\nOwner: " + vill.VillageOwner.Name;

                    Tribe t = player.Tribe;
                    if (t != null)
                    {
                        s += "\nTribe: " + t.Tag + " (" + t.Name + ")";
                    }
                    else
                    {
                        s += "\nTribe:";
                    }
                }
                else 
                {
                    s += "\nOwner:" ;
                    s += "\nTribe:";
                }
            }
            return s;
        }

        public static string FormatVillageForDisplay_Short(Village vill)
        {
            string s = "";
            if (vill != null)
            {
                Player player = vill.VillageOwner;
                s = vill.name + " - (" + vill.X.ToString() + "|" + vill.y.ToString() + ")";
            }
            return s;
        }
 
    }



    public delegate void AppUpdatedDelegate();

    
}
